import { prisma } from '@/lib/prisma';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/app/api/auth/[...nextauth]/route';
import { PopularTripCard } from '@/app/components/PopularTripCard';
import { redirect } from 'next/navigation';

async function getSavedTrips() {
  const session = await getServerSession(authOptions);
  if (!session?.user?.id) return [];

  return await prisma.trip.findMany({
    where: {
      savedBy: {
        some: { id: session.user.id }
      }
    },
    include: {
      activities: true,
      savedBy: true,
      _count: {
        select: { activities: true }
      }
    },
    orderBy: {
      createdAt: 'desc'
    }
  });
}

export default async function SavedTripsPage() {
  const session = await getServerSession(authOptions);
  if (!session) {
    redirect('/login');
  }

  const trips = await getSavedTrips();

  return (
    <div className="max-w-7xl mx-auto py-8 px-4 sm:px-6 lg:px-8">
      <h1 className="text-3xl font-bold text-blue-900 mb-8">我的收藏</h1>
      {trips.length === 0 ? (
        <div className="text-center py-12 bg-gray-50 rounded-lg">
          <p className="text-gray-500">还没有收藏任何行程</p>
        </div>
      ) : (
        <div className="grid gap-6 md:grid-cols-2 lg:grid-cols-3">
          {trips.map((trip) => (
            <PopularTripCard key={trip.id} trip={trip} />
          ))}
        </div>
      )}
    </div>
  );
} 